import {Component} from '@angular/core';
import {LoginForm} from "../../../user/model/loginForm";
import {UserService} from "../../../user/service/user.service";
import {PromptService} from "../../../util/prompt/prompt.service";
import {Router} from "@angular/router";
import {CommonService} from "../../../util/common.service";
import {CurrentUserInfo} from "../../../user/model/currentUserInfo";

@Component({
    moduleId: module.id,
    selector: 'login-component',
    templateUrl: "./login.html"
})


export class LoginComponent {
    loginForm: LoginForm = new LoginForm()

    constructor(private userService: UserService, private promptService: PromptService, private router: Router, public commonService: CommonService) {

    }

    login() {

        this.userService.login(this.loginForm).then((data: any)=> {
            if (data.success) {
                console.log(data.result)
                let userInfo: CurrentUserInfo = Object.assign(new CurrentUserInfo(), data.result)
                this.promptService.info("登录成功")
                if (userInfo.isPermissionOfUserEdit()) {
                    this.router.navigate(["/console/user/list"]);
                } else if (userInfo.isPermissionOfFillPlanFill()) {
                    this.router.navigate(["/console/fillPlan/task/fillList"]);
                } else if (userInfo.isPermissionOfFillPlanCheck()) {
                    this.router.navigate(["/console/fillPlan/task/checkList"]);
                } else {
                    this.router.navigate(["/console/index"]);
                }
            } else {
                this.promptService.warning(data.error.message)
            }
        })
    }

}